<?php

#error_reporting(0);
/*
 * Copyright (C) 2014 koodo@qq.com.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */

switch ($_POST['a']) {
    // 数据库连接检查
    case 'db_valid' : {
            if (mysql_connect($_POST['f-dbaddress'], $_POST['f-dbusername'], $_POST['f-dbpassword'])) {
                echo 1;
            } else {
                echo 0;
            }
        }
        break;
    // 数据库安装
    case 'db_install': {
            $db = mysql_connect($_POST['f-dbaddress'], $_POST['f-dbusername'], $_POST['f-dbpassword']);
            mysql_query("drop database if exists " . $_POST['f-dbname'] . ";");
            $db_found = mysql_query("CREATE DATABASE IF NOT EXISTS " . $_POST['f-dbname'] . " DEFAULT CHARSET utf8 COLLATE utf8_general_ci;") !== false;
            if ($db_found) {
                // 数据库处理完成，导入数据
                mysql_select_db($_POST['f-dbname']);
                mysql_query("SET NAMES 'utf8';");
                // 使用事务进行导入
                mysql_query("START TRANSACTION;");
                $sqls = loadWshopSql();
                foreach ($sqls as $index => $sql) {
                    if ($sql != '') {
                        if (mysql_query($sql) === false) {
                            // 遇到错误回滚
                            mysql_query("ROLLBACK");
                            mysql_query("END");
                            die(0);
                        }
                    }
                }
                mysql_query("COMMIT;");
                mysql_query("END");
                echo 1;
            } else {
                echo -1;
            }
        }
        break;
    case 'config_install': {
            $config = '../config/config.php';
            unlink($config);
            touch($config);
            file_put_contents($config, "<?php" . PHP_EOL);
            file_put_contents($config, "include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sys_config.php';" . PHP_EOL, FILE_APPEND);
            file_put_contents($config, 'define("APPID", "' . $_POST['f-appid'] . '");' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, 'define("APPSECRET", "' . $_POST['f-appsecret'] . '");' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, 'define("TOKEN", "' . $_POST['f-token'] . '");' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, 'define("PARTNER", "' . $_POST['f-partner'] . '");' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, 'define("APPKEY", "' . $_POST['paysignkey'] . '");' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, 'define("PARTNERKEY", "' . $_POST['f-partnerkey'] . '");' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, 'define("SIGNTYPE", "sha1");' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, '$config->debug = true;' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, '$config->db[\'host\'] = \'' . $_POST['f-dbaddress'] . '\';' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, '$config->db[\'db\'] = \'' . $_POST['f-dbname'] . '\';' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, '$config->db[\'user\'] = \'' . $_POST['f-dbusername'] . '\';' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, '$config->db[\'pass\'] = \'' . $_POST['f-dbpassword'] . '\';' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, '$config->memcached[\'port\'] = 11211;' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, '$config->memcached[\'exps\'] = 3;' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, '$config->Smarty[\'cached\'] = true;' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, '$config->Smarty[\'cache_lifetime\'] = 3;' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, '$config->shoproot = \'/\';' . PHP_EOL, FILE_APPEND);
            file_put_contents($config, '$config->order_nopayment = true;' . PHP_EOL, FILE_APPEND);
            touch('install.lock');
            unset($config);

            // 创建admin账户
            include '../config/sys_config.php';
            $db = mysql_connect($_POST['f-dbaddress'], $_POST['f-dbusername'], $_POST['f-dbpassword']);
            mysql_query("SET NAMES 'utf8';");
            mysql_select_db($_POST['f-dbname']);
            $pwd = hash('sha384', $_POST['f-adminpassword'] . $config->admin_salt . hash('md4', $config->admin_salt2[intval($_POST['f-adminname'])]));
            mysql_query("INSERT INTO `admin` (admin_account,admin_password,admin_permission) VALUES ('" . $_POST['f-adminname'] . "','$pwd',0);");
            if (!is_dir('../tmp')) {
                mkdir('../tmp', 744);
            }
            echo 1;
        }
}

// 对navicat导出的sql文件进行数组拆分
function loadWshopSql() {
    $lines = file("iwshop.sql");
    $sqlstr = "";
    foreach ($lines as $line) {
        if ($line != "") {
            if (!($line{0} == "#" || $line{0} . $line{1} == "--")) {
                $sqlstr.=$line;
            }
        }
    }
    $sqls = explode(";", $sqlstr);
    # file_put_contents('x', print_r($sqls,true));
    return $sqls;
}
